﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace HoaDon
{
	public partial class DM_Ban : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				LoadDropDownList();
				BindData();
				lbError.Text = "";
			}
		}

		private void BindData()
		{
			if (drkhuvuc.Items.Count <= 0)
				return;
			string nhvvid = drkhuvuc.SelectedValue.ToString();
			DataTable dt = new DataTable();
			dt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM vdmvv WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND FK_NhvvID = '" + nhvvid + "' AND Status = '1'");

			gdvban.DataSource = dt;
			gdvban.DataBind();
			if (dt.Rows.Count == 0)
			{
				DataRow dtr = dt.NewRow();
				dt.Rows.Add(dtr);
				gdvban.DataSource = dt;
				gdvban.DataBind();
				gdvban.Rows[0].Enabled = false;
			}
		}

		private void LoadDropDownList()
		{
			DataTable dt = new DataTable();
			dt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM dmnhvv WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Status = '1'");
			drkhuvuc.DataSource = dt;
			drkhuvuc.DataValueField = "nhvvID";
			drkhuvuc.DataTextField = "nhvvName";
			drkhuvuc.DataBind();
			if (drkhuvuc.Items.Count > 0)
				drkhuvuc.SelectedIndex = 0;
		}

		protected void EditDM(object sender, GridViewEditEventArgs e)
		{
			gdvban.EditIndex = e.NewEditIndex;
			BindData();
			lbError.Text = "";
		}
		protected void CancelEditDM(object sender, GridViewCancelEditEventArgs e)
		{
			gdvban.EditIndex = -1;
			BindData();
			lbError.Text = "";
		}

		protected void OnPaging(object sender, GridViewPageEventArgs e)
		{
			BindData();
			gdvban.PageIndex = e.NewPageIndex;
			gdvban.DataBind();
			lbError.Text = "";
		}

		protected void UpdateDM(object sender, GridViewUpdateEventArgs e)
		{
			string id = ((Label)gdvban.Rows[e.RowIndex].FindControl("vvID")).Text;
			string name = ((TextBox)gdvban.Rows[e.RowIndex].FindControl("tbvvName")).Text;
			if (id == "")
			{
				lbError.Text = "Không để trống mã";
				return;
			}
			if (name == "")
			{
				lbError.Text = "Không để trống tên";
				return;
			}

			SqlEXEC.ExecCommand(Session["strConn"].ToString(), "UPDATE dmvv SET vvName = N'" + name + "',user_id2='" + System.Web.HttpContext.Current.User.Identity.Name.ToUpper() + "',datetime2=GETDATE() WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND vvID= '" + id + "'");
			gdvban.EditIndex = -1;
			BindData();
			lbError.Text = "";
		}

		protected void DeleteDM(object sender, EventArgs e)
		{
			Button bt = (Button)sender;
			try
			{
				SqlEXEC.ExecCommand(Session["strConn"].ToString(), "delete from dmvv WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND vvID= '" + bt.CommandArgument + "'");
			}
			catch (Exception ex)
			{
				lbError.Text = "Đã phát sinh dữ liệu không thể xóa";
			}
			BindData();
		}
		protected void AddNewDM(object sender, EventArgs e)
		{
			try
			{
				if (drkhuvuc.Items.Count <= 0)
				{
					lbError.Text = "Chưa chọn khu vực, tầng";
					return;
				}
				string Nhvvid = drkhuvuc.SelectedValue.ToString();
				string id = ((TextBox)gdvban.FooterRow.FindControl("txtvvID")).Text;
				string name = ((TextBox)gdvban.FooterRow.FindControl("txtvvName")).Text;
				if (id == "")
				{
					lbError.Text = "Không để trống mã";
					return;
				}
				if (name == "")
				{
					lbError.Text = "Không để trống tên";
					return;
				}

				if (SqlEXEC.ExecCommand(Session["strConn"].ToString(), "INSERT INTO dmvv (vvID,FK_DatabaseID,vvName,Status,user_id0,user_id2,datetime0,datetime2,FK_NhvvId) VALUES ('" + id + "','" + Session["FK_DatabaseID"].ToString() + "',N'" + name + "','1','" + System.Web.HttpContext.Current.User.Identity.Name.ToUpper() + "','" + System.Web.HttpContext.Current.User.Identity.Name.ToUpper() + "',GETDATE(),GETDATE(),'" + Nhvvid + "')") == -1)
					lbError.Text = "Mã đã tồn tại, chọn mã khác";
				else
				{
					BindData();
					lbError.Text = "";
				}
			}
			catch (Exception ex)
			{
				lbError.Text = "Phải chọn khu vực!";
				return;
			}
		}

		protected void drkhuvuc_SelectedIndexChanged(object sender, EventArgs e)
		{
			BindData();
			lbError.Text = "";
		}
	}
}
